package com.analog.mserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.analog.mserver.common.RestResult;
import com.analog.mserver.jwt.JwtUtil;
import com.analog.mserver.service.user.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api("LoginController相关的api")
public class LoginController {
	
	@Autowired
	public UserService userService;

	
	@ApiOperation(value = "login", notes = "login")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "username", value = "username", required = true, dataType = "String", paramType = "path")
		,
		@ApiImplicitParam(name = "password", value = "password", required = true, dataType = "String", paramType = "path")
	})
	@GetMapping("login/{username}/{password}")
	public RestResult login(@PathVariable String username,@PathVariable String password) {
		
		if(userService.login(username,password)) {
			return new RestResult(JwtUtil.sign(username, password));
		}else {
			return new RestResult("2", "用户名或密码错误");
		}
		
	}
	
	
}
